//
//  MOOpenAppViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/7/5.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOOpenAppViewController.h"
#import "MOGestureOpenViewController.h"

@interface MOOpenAppViewController ()

@property(nonatomic,strong)UITextField *inputTextField;

@property(nonatomic,strong)UIButton *openButton;

@end

@implementation MOOpenAppViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self creatView];
    
    [self getData];
}

-(void)creatView
{
    self.view.backgroundColor = KGrayColor_ededed;
    
    UIImageView *iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, kWConversions(50), SCREEN_WIDTH/2, SCREEN_WIDTH/2)];
    iconImageView.centerX = SCREEN_WIDTH/2;
    iconImageView.image = IMAGE(@"openHeader");
    [self.view addSubview:iconImageView];
    
    self.inputTextField = [[UITextField alloc]initWithFrame:CGRectMake(kWConversions(40), iconImageView.bottom + 20, SCREEN_WIDTH - kWConversions(40)*2, 40)];
    self.inputTextField.layer.masksToBounds = YES;
    self.inputTextField.layer.cornerRadius = 3.0;
    self.inputTextField.backgroundColor = [UIColor whiteColor];
    self.inputTextField.placeholder = @"请输入密码";
    [self.view addSubview:self.inputTextField];
    
    UIView *left = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 8 )];
    self.inputTextField.leftView = left;
    self.inputTextField.leftViewMode = UITextFieldViewModeAlways;
    
    self.openButton = [[UIButton alloc]initWithFrame:CGRectMake(kWConversions(40), self.inputTextField.bottom + 15, SCREEN_WIDTH - kWConversions(40)*2, 40)];
    [self.openButton setTitle:@"解锁" forState:UIControlStateNormal];
    [self.openButton addTarget:self action:@selector(clickOpenButton) forControlEvents:UIControlEventTouchUpInside];
    self.openButton.titleLabel.font = FONT_SIZE(15);
    self.openButton.layer.masksToBounds = YES;
    self.openButton.layer.cornerRadius = 3.0;
    self.openButton.backgroundColor = KBlueColor_2C94FF;
    [self.view addSubview:self.openButton];
    
    
    
}

-(void)getData
{
    MOUser *user = [MOUser sharedMOUser];//0124678
    
    NSString *postUrl = [NSString stringWithFormat:@"%@/%@?authorization=%@",FINDLOCKERBYUSER_URL,user.userName,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [PPNetworkHelper GET:postUrl parameters:@{} success:^(id responseObject) {
        NSLog(@"查询手势密码:%@",responseObject);
        if ([responseObject[@"enable"] integerValue]==1) {//0：关闭 1：开启
            
            UIButton *getstureOpenButton = [[UIButton alloc]initWithFrame:CGRectMake(kWConversions(40), self.openButton.bottom + 15, 100, 40)];
            [getstureOpenButton setTitle:@"手势解锁" forState:UIControlStateNormal];
            [getstureOpenButton addTarget:self action:@selector(clickGestureButton) forControlEvents:UIControlEventTouchUpInside];
            [getstureOpenButton setTitleColor:KBlueColor_2C94FF forState:UIControlStateNormal];
            getstureOpenButton.titleLabel.font = FONT_SIZE(15);
            [getstureOpenButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentLeft)];
            [self.view addSubview:getstureOpenButton];
        
        }
    } failure:^(NSError *error) {
    }];
}


//解锁
-(void)clickOpenButton
{
    MOUser *user = [MOUser sharedMOUser];
    NSString *pas = [self sha1:[NSString stringWithFormat:@"%@%@",self.inputTextField.text,user.userName]];
    
    if ([pas isEqualToString:user.password]) {
        AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [appDelegate creatTabbar];
    }else{
        [[ADShowMessage shareShowMessage]showTextOnly:@"密码校验失败" showInView:self.view];
    }
}

//手势解锁
-(void)clickGestureButton
{
    MOGestureOpenViewController *vc = [[MOGestureOpenViewController alloc]init];
    [self presentViewController:vc animated:YES completion:^{
        
    }];
}
                                
#pragma mark -//sha1加密方式

- (NSString *) sha1:(NSString *)input
    {
        //这两句容易造成 、中文字符串转data时会造成数据丢失
        //const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
        //NSData *data = [NSData dataWithBytes:cstr length:input.length];
        //instead of
        NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding];
        
        uint8_t digest[CC_SHA1_DIGEST_LENGTH];
        
        CC_SHA1(data.bytes, (unsigned int)data.length, digest);
        
        NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
        
        for(int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
            [output appendFormat:@"%02x", digest[i]];
        }
        return output;
    }








@end
